package com.tanhua.server.service;

import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;

import com.tanhua.dubbo.api.RideoApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Rideo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.RideoVo;
import com.tanhua.server.exeception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author SunSun
 * @date 2021/11/7
 */
@Service
public class RideoService {


    @Autowired
    private FastFileStorageClient client;

    @Autowired
    private FdfsWebServer webServer;

    @DubboReference
    private RideoApi rideoApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    public void publishRideo(MultipartFile soundFile) throws IOException {
        if(soundFile.isEmpty()){
            throw new BusinessException(ErrorResult.error());
        }
        String rideoName = soundFile.getOriginalFilename();
        rideoName= rideoName.substring(rideoName.lastIndexOf(".") + 1);
        StorePath storePath = client.uploadFile(soundFile.getInputStream(), soundFile.getSize(), rideoName, null);
        String redeoUrl =webServer.getWebServerUrl()+storePath.getFullPath();

        Rideo rideo=new Rideo();
        rideo.setCreated(System.currentTimeMillis());
        rideo.setRideoUrl(redeoUrl);
        rideo.setUserId(UserHolder.getUserId());

        String rideoId=rideoApi.upload(rideo);
        if(rideoId.isEmpty()){
            throw new BusinessException(ErrorResult.error());
        }
    }

    public RideoVo selectOne() {
        Rideo rideo=rideoApi.randomRideo();
        if(rideo==null){
            throw  new BusinessException(ErrorResult.error());
        }
        Long userId = rideo.getUserId();
        UserInfo userInfo = userInfoApi.findByid(userId);
        RideoVo vo=null;
        if (userInfo!=null){
            vo = RideoVo.init(userInfo, rideo);
        }
        String key="RIDEO_TIME";
        if (!redisTemplate.hasKey(key)){
            redisTemplate.opsForValue().set(key,"9",24*60,TimeUnit.MINUTES);
            vo.setRemainingTimes(10);
            return vo;
        }else {
            String value = redisTemplate.opsForValue().get(key);
            Integer i = Integer.parseInt(value);
            if (i>0){
                String value1 = String.valueOf(i - 1);
                redisTemplate.delete(key);
                redisTemplate.opsForValue().set(key,value1,24*60,TimeUnit.MINUTES);
                vo.setRemainingTimes(i-1);
                return vo;
            }
            return null;
        }
    }
}
